#!/bin/sh

set -e
set -x

#################################
# AUTOMATICALLY DETECT ENV HERE #
#################################

if ! [ -r /etc/pkgos/pkgos.conf ] ; then
	echo "Could not read /etc/pkgos/pkgos.conf"
	exit 1
else
	. /etc/pkgos/pkgos.conf
fi

detect_env () {
	DEB_RELEASE=`lsb_release -a | grep Codename: | awk '{print $2}'`
	DEB_RELEASE_NUM=`lsb_release -a | grep Release: | awk '{print $2}'`
	DEB_DIST_TYPE=$(lsb_release -a | grep "Distributor ID:" | awk '{print $3}')
	APT="apt-get install -y"
	echo 'APT::Install-Recommends "0";' >/etc/apt/apt.conf.d/80norecommends
}

build_ostack_archive_keyring_package () {
#	# Export the jenkins GPG key as pubkey.gpg in debian/dists/pubkey.gpg
#	mkdir -p ${REPO_ROOT}/debian/dists
#	chown -R ${THE_DEV_USER}:${THE_DEV_USER} ${REPO_ROOT}/debian/dists
#	chown ${THE_DEV_USER}:${THE_DEV_USER} ${REPO_ROOT}/debian
#	su ${THE_DEV_USER} -c "gpg --export -a ${THE_DEV_USER}" >${REPO_ROOT}/debian/dists/pubkey.gpg

	# Create a Debian package out of it, called openstack-debian-archive-keyring
	# and put it in the newly created Debian repository.
	# Yes, a working, Debian-policy-compliant package is really only a few lines of shell... :)
	TMPDIR=`mktemp -d`
	MYCWD=`pwd`
	cd ${TMPDIR}
	VER=0.1
	NAME=openstack-backports-archive-keyring
	rm -rf ${NAME}-${VER}
	mkdir -p ${NAME}-${VER}/debian/source
	cd ${NAME}-${VER}
	export DEBFULLNAME="Autogenerated key"
	export DEBEMAIL="${THE_DEV_USER}@${HOST_FQDN}"
	dch --create --package ${NAME} -D unstable --noquery --newversion 0.1 -m "Automatic archive package build."
	sed -i 's/MAINTAINER <EMAIL>/Autogenerated key <'${THE_DEV_USER}'@'${HOST_FQDN}'>/' debian/changelog
	echo "3.0 (native)" >debian/source/format
	echo 9 >debian/compat
	echo "#!/usr/bin/make -f
%:
	dh \$@
" >debian/rules
	echo "Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: ${NAME}
Source: See the openstack-pkg-tools package

Files: *
Copyright: (c) 2015, Thomas Goirand <zigo@debian.org>
License: Apache-2
 Licensed under the Apache License, Version 2.0 (the \"License\");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 .
    http://www.apache.org/licenses/LICENSE-2.0
 .
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an \"AS IS\" BASIS,  
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 .
 On Debian-based systems the full text of the Apache version 2.0 license
 can be found in /usr/share/common-licenses/Apache-2.0.
" >debian/copyright
	echo "Source: ${NAME}
Section: net
Priority: extra
Maintainer: PKG OpenStack <openstack-devel@lists.alioth.debian.org>
Uploaders: Thomas Goirand <zigo@debian.org>
Build-Depends: debhelper (>= 9)
Standards-Version: 3.9.6

Package: ${NAME}
Architecture: all
Depends: gnupg, \${misc:Depends}
Description: OpenStack ${TARGET_OPENSTACK_REL} ${TARGET_DISTRO} archive keyring
 OpenStack ${TARGET_OPENSTACK_REL} ${TARGET_DISTRO} archive keyring
">debian/control
	#su ${THE_DEV_USER} -c "gpg --export -a ${THE_DEV_USER}" >pubkey.gpg
	gpg --export -a >pubkey.gpg
	echo "pubkey.gpg /usr/share/${NAME}" >debian/${NAME}.install
	echo "#!/bin/sh
set -e

if [ \"\$1\" = \"configure\" ] || [ \"\$1\" = \"reconfigure\" ] ; then
	apt-key add /usr/share/${NAME}/pubkey.gpg
fi

#DEBHELPER#

exit 0
" >debian/${NAME}.postinst
	#chown -R ${THE_DEV_USER}:${THE_DEV_USER} .
	#su ${THE_DEV_USER} -c dpkg-buildpackage || true
	dpkg-buildpackage
	FIRST_LETTER=`echo ${NAME} | awk '{print substr($0,0,2)}'`
	cd ..
	MYDEST=${REPO_ROOT}/debian/pool/${REPO_DEST}/main/${FIRST_LETTER}/${NAME}
	MYDEST2=${REPO_ROOT}/debian/pool/${REPO_NOCHANGE_BACKPORT_DEST}/main/${FIRST_LETTER}/${NAME}
	for i in ${MYDEST} ${MYDEST2} ; do
		mkdir -p ${i}
		cp *.changes *.tar.xz *.deb *.dsc ${i}
	done
	cd ${MYCWD}
	rm -r ${TMPDIR}
}

HOST_FQDN=`hostname --fqdn`
THE_DEV_USER=jenkins
build_ostack_archive_keyring_package
